 import _ from 'lodash';
   import { toRaw } from 'vue';
export class Tool {
  /**
   * 空校验 null或""都返回true
   */
  /**
   * 空校验
   */
  public static isEmpty(obj: any): boolean {
    if (obj == null) return true;
    if (typeof obj === 'string') return obj.trim() === "";
    if (Array.isArray(obj)) return obj.length === 0;
    if (typeof obj === 'object') return Object.keys(toRaw(obj)).length === 0;
    return false;
  }

  /**
   * 非空校验
   */
  public static isNotEmpty(obj: any): boolean {
    return !this.isEmpty(obj);
  }

  /**
   * 对象复制（安全版）
   */
  public static copy<T>(obj: T): T {
    if (!obj) return obj;
    return JSON.parse(JSON.stringify(toRaw(obj)));
  }
  

  /**
   * 使用递归将数组转为树形结构
   * 父ID属性为parent
   */
  public static array2Tree (array: any, parentId: number) {
    if (Tool.isEmpty(array)) {
      return [];
    }
    const result = [];
    for (let i = 0; i < array.length; i++) {
      const c = array[i];
       //console.log(String(c.parent), String(parentId));
      if (String(c.parent) === String(parentId)) {
        result.push(c);

        // 递归查看当前节点对应的子节点
        const children = Tool.array2Tree(array, c.id);
        if (Tool.isNotEmpty(children)) {
          c.children = children;
        }
      }
    }
    return result;
  }

  /**
   * 随机生成[len]长度的[radix]进制数
   * @param len
   * @param radix 默认62
   * @returns {string}
   */
  public static uuid (len: number, radix = 62) {
    const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    const uuid = [];
    radix = radix || chars.length;

    for (let i = 0; i < len; i++) {
      uuid[i] = chars[0 | Math.random() * radix];
    }

    return uuid.join('');
  }
}
